/**
 * Jackeel properties
 */
package com.dd.keel.core.loader.bootstrap;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;


/**
 * Utility class to read the bootstrap Jackeel configuration.
 *
 * @author Kevin.XU
 * @version 
 */

public class JackeelProperties {

    // ------------------------------------------------------- Static Variables
    private static Properties properties = null;

    static {
        loadProperties();
    }

    // --------------------------------------------------------- Public Methods
    /**
     * Return specified property value.
     */
    public static String getProperty(String name) {
    	return System.getProperty(name,properties.getProperty(name));
        //return properties.getProperty(name);
    }

    /**
     * Return specified property value.
     */
    public static String getProperty(String name, String defaultValue) {
    	return System.getProperty(name,properties.getProperty(name, defaultValue));
        //return properties.getProperty(name, defaultValue);
    }

    // --------------------------------------------------------- Public Methods
    /**
     * Load properties.
     */
    private static void loadProperties() {

        InputStream is = null;
        Throwable error = null;

        try {
            String configUrl = getConfigUrl();
            if (configUrl != null) {
                is = (new URL(configUrl)).openStream();
            }
            System.out.println("Found "+getJackeelHome()+"/jackeel.properties!");
        } catch (Throwable t) {
        	//System.out.println("Not found "+getJackeelHome()+"/jackeel.properties!");
        }
        
        if (is == null) {
            try {
                File home = new File(getJackeelHome());
                File conf = new File(home, "etc");
                File properties = new File(conf, "jackeel.properties");
                is = new FileInputStream(properties);
                System.out.println("Found "+getJackeelHome()+"/etc/jackeel.properties!");
            } catch (Throwable t) {
                //System.out.println("Not found "+getJackeelHome()+"/etc/jackeel.properties!");
            }
        }     
        
        if (is == null) {
            try {
                is = JackeelProperties.class.getResourceAsStream("/jackeel.properties");
                System.out.println("Found jackeel.properties in keel-core-loader-x.x.x.jar!");
            } catch (Throwable t) {
            	//System.out.println("Not found jackeel.properties on classpath!");
            }
        }
        
        if (is != null) {
            try {
                properties = new Properties();
                properties.load(is);
                is.close();
            } catch (Throwable t) {
                error = t;
            }
        }
        else
        {
        	System.out.println("Not found jackeel.properties!");
        }
        
        if ((is == null) || (error != null)) {
            // That's fine - we have reasonable defaults.
            properties=new Properties();
        }
        
        //System.out.println("properties="+properties);
    }

    /**
     * Get the value of the keel.home environment variable.
     */
    public static String getJackeelHome() {
        return System.getProperty("keel.home",System.getProperty("user.dir"));
    }
    
    /**
     * Get the value of the configuration URL.
     */
    public static String getConfigUrl() {
        return getJackeelHome()+"/jackeel.properties";
    }

}
